package com.litorg.demo.db.config;
 
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.rabbit.connection.ConnectionFactory;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.amqp.support.converter.Jackson2JsonMessageConverter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
@ConditionalOnProperty(prefix = "assembly-switch", name = "rabbitmq", havingValue = "true")
public class MQConfig {
    //将数据自动的转为json发送出去
    @Bean
    public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory) {
        RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
        rabbitTemplate.setMessageConverter(new Jackson2JsonMessageConverter());
        return rabbitTemplate;
    }
 
    /**
     * 申明一个FanoutExchange
     * 名字：hot_send_fanout_exchange
     * 开启持久化
     * 自动删除关闭
     *
     * @return
     */
    @Bean
    public FanoutExchange e1() {
        return new FanoutExchange("hot_send_fanout_exchange", true, false);
    }
 
    /**
     * 申明一个队列
     * 名字：q1
     *
     * @return
     */
    @Bean
    public Queue q1() {
        return new Queue("q1", true);
    }
 
    /**
     * 申明一个队列
     * 名字：q2
     *
     * @return
     */
    @Bean
    public Queue q2() {
        return new Queue("q2", true);
    }
 
    /**
     * 将q1 绑定到 hot_send_fanout_exchange
     *
     * @return
     */
    @Bean
    public Binding bin1() {
        return BindingBuilder.bind(q1()).to(e1());
    }
 
    /**
     * 将q2 绑定到 hot_send_fanout_exchange
     *
     * @return
     */
    @Bean
    public Binding bin2() {
        return BindingBuilder.bind(q2()).to(e1());
    }
 
}